package at.codejedi.sto.gui.combat;

import java.awt.GridLayout;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.swing.JPanel;
import javax.swing.JTabbedPane;

import at.codejedi.sto.config.Configuration;
import at.codejedi.sto.gui.detail.chart.BattleBarChart;
import at.codejedi.sto.gui.detail.chart.BattleBarChartData;
import at.codejedi.sto.gui.lazy.LazyPanel;
import at.codejedi.sto.model.combat.IPlayer;
import at.codejedi.sto.model.combat.IPlayerBattle;

public class PlayerDetailView extends LazyPanel implements IDetailView {

	private static final long serialVersionUID = 2444340248916324202L;

	EntryView entryView;
	BattleBarChart netAllChart;
	DamagePanel netDamagePanel;
	BattleBarChart netDPSChart;
	BattleBarChart rawAllChart;
	DamagePanel rawDamagePanel;

	BattleBarChart rawDPSChart;

	BattleStatisticsPanel statisticsPanel;

	public PlayerDetailView() {
		setLayout(new GridLayout(0, 1, 0, 0));
		JTabbedPane tabPane = new JTabbedPane();
		add(tabPane);

		JPanel chartPanel = new JPanel();

		netDPSChart = new BattleBarChart("Net DPS Battles");
		rawDPSChart = new BattleBarChart("Raw DPS Battles");
		netAllChart = new BattleBarChart("Net All Damage Battles");
		rawAllChart = new BattleBarChart("Raw All Damage Battles");

		chartPanel.setLayout(new GridLayout(2, 2, 0, 0));
		chartPanel.add(netDPSChart);
		chartPanel.add(rawDPSChart);
		chartPanel.add(netAllChart);
		chartPanel.add(rawAllChart);
		tabPane.addTab(
				Configuration.getLabel("BattleStatistics.Tabs.Overview"),
				chartPanel);

		statisticsPanel = new BattleStatisticsPanel();
		tabPane.addTab(
				Configuration.getLabel("BattleStatistics.Tabs.Statistics"),
				statisticsPanel);

		entryView = new EntryView();
		tabPane.addTab(Configuration.getLabel("BattleStatistics.Tabs.RawData"),
				entryView);
	}

	public void displayPlayer(IPlayer p) {

		statisticsPanel.setStatistics(p);

		List<BattleBarChartData> netDPSData = new ArrayList<BattleBarChartData>();
		List<BattleBarChartData> rawDPSData = new ArrayList<BattleBarChartData>();
		List<BattleBarChartData> netAllDamageData = new ArrayList<BattleBarChartData>();
		List<BattleBarChartData> rawAllDamageData = new ArrayList<BattleBarChartData>();

		for (IPlayerBattle battle : p.getBattles()) {
			BattleBarChartData netDPS = new BattleBarChartData();
			netDPS.name = getBattleName(battle);
			netDPS.dealt = battle.getNetDamageDealt().getDamagePerSecond();
			netDPS.received = battle.getNetDamageReceived()
					.getDamagePerSecond();
			netDPSData.add(netDPS);

			BattleBarChartData rawDPS = new BattleBarChartData();
			rawDPS.name = getBattleName(battle);
			rawDPS.dealt = battle.getRawDamageDealt().getDamagePerSecond();
			rawDPS.received = battle.getRawDamageReceived()
					.getDamagePerSecond();
			rawDPSData.add(rawDPS);

			BattleBarChartData netAllDamage = new BattleBarChartData();
			netAllDamage.name = getBattleName(battle);
			netAllDamage.dealt = battle.getNetDamageDealt().getAllDamage();
			netAllDamage.received = battle.getNetDamageReceived()
					.getAllDamage();
			netAllDamageData.add(netAllDamage);

			BattleBarChartData rawAllDamage = new BattleBarChartData();
			rawAllDamage.name = getBattleName(battle);
			rawAllDamage.dealt = battle.getRawDamageDealt().getAllDamage();
			rawAllDamage.received = battle.getRawDamageReceived()
					.getAllDamage();
			rawAllDamageData.add(rawAllDamage);
		}

		netDPSChart.init(netDPSData);
		rawDPSChart.init(rawDPSData);
		netAllChart.init(netAllDamageData);
		rawAllChart.init(rawAllDamageData);

		netDPSChart.setVisible(false);
		netDPSChart.setVisible(true);

		entryView.display(p);

	}

	private String getBattleName(IPlayerBattle battle) {
		Calendar start = Calendar.getInstance();
		start.setTimeInMillis(battle.getBeginningMillis());

		DateFormat sdf = Configuration.getDateTimeFormat();
		return sdf.format(start.getTime());
	}
}
